package TabbedLesson
{
	import Assets.Controls.NewTab;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mvc.AbstractView;
	import mvc.Controller;
	
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.List;
	import mx.events.CloseEvent;
	import mx.events.ListEvent;
	
	import util.Observable;

	public class TabbedLessonView extends AbstractView
	{
		private var lessonName_string:String;
		private var lessonTab:NewTab;
		private var nativeContainer_sprite:Sprite;
		private var tabLessoncontroller:Object;
		
		private var editLesson_button:Button;
		private var editMode_bool:Boolean;
		private var component_button:Button;
		private var component_list:List;
		private var addComponent_button:Button;
		private var compName_array:Array;
		
		[Embed(source="Assets/icons/LessonBack.jpg")]
		[Bindable]
		private var lessonTabBackground:Class;
		
		
		public function TabbedLessonView(model:Observable, controller:Controller, container:Sprite, title:String)
		{
			super(model, controller);
			this.nativeContainer_sprite = container;
			this.lessonName_string = title;
			this.tabLessoncontroller = this.getController();
			this.initLessonTab();
		}
		
		private function initLessonTab():void
		{
			var infoObj_object:Object = new Object;
			infoObj_object.tabName = this.lessonName_string;
			//infoObj_object.children = this.children_array;
			infoObj_object.tabBackground = this.lessonTabBackground;
			this.lessonTab = new NewTab(this.nativeContainer_sprite);
			this.lessonTab.setTabAppearance(infoObj_object);
			
			this.editLesson_button = new Button;
			this.editLesson_button.label = "Edit";
			this.editLesson_button.x=10;
			this.editLesson_button.y=10;
			this.editLesson_button.addEventListener(MouseEvent.CLICK, this.initEditMode);
			this.addChildToStage(this.editLesson_button);
			
			this.component_button = new Button;
			this.component_button.label = "components";
			this.component_button.x=10;
			this.component_button.y=40;
			this.component_button.visible=false;
			this.component_button.addEventListener(MouseEvent.CLICK,this.initComponentList);
			this.addChildToStage(this.component_button);
			
			
			this.compName_array = new Array;
			this.compName_array = (["Glossary","Slides","Audio","Chat","Video","Topics","ScratchPad","Assessment","PasteNote"]);
			//this.compName_array.push("Video", "Slides");			
			this.component_list = new List;
			this.component_list.dataProvider = this.compName_array;
			this.component_list.validateNow();
			this.component_list.editable = false;
			this.component_list.visible = false;
			this.component_list.selectable = true;
			this.component_list.x=10;
			this.component_list.y=70;
			this.component_list.width=100;
			this.component_list.height = 300;
			this.component_list.addEventListener(ListEvent.ITEM_CLICK,this.listItemSelected);
			this.addChildToStage(this.component_list);
			
			this.addComponent_button = new Button;
			this.addComponent_button.label = "AddComponent";
			this.addComponent_button.x=10;
			this.addComponent_button.y=this.component_list.y+this.component_list.height+10;
			this.addComponent_button.visible=false;
			this.addComponent_button.addEventListener(MouseEvent.CLICK,this.addComponent);
			this.addChildToStage(this.addComponent_button);
			
			this.addMouseUpEventToTab();
		}
		
		private function initEditMode(event:MouseEvent):void
		{
			this.editLesson_button.label="Save";
			this.editLesson_button.removeEventListener(MouseEvent.CLICK,this.initEditMode);
			this.editLesson_button.addEventListener(MouseEvent.CLICK,this.saveLesson);
			this.component_button.visible=true;
			
			this.editMode_bool = true;
			
			this.informEditModeToModel();
		}
		
		private function saveLesson(event:MouseEvent):void
		{
			this.editLesson_button.label="Edit";
			this.editLesson_button.removeEventListener(MouseEvent.CLICK, this.saveLesson);
			this.editLesson_button.addEventListener(MouseEvent.CLICK, this.initEditMode);
			this.component_button.visible=false;
			this.component_list.visible=false;
			
			this.editMode_bool=false;
			this.informEditModeToModel();
			
			var infoObj_object:Object = new Object;
			infoObj_object.message = "saveLesson";
			this.sendInfoToController(infoObj_object);
		}
		
		//------------------------------------events to enable resizing --------------------------------------------------------------//
		
		private function addMouseUpEventToTab():void
		{
			this.lessonTab.newTab.addEventListener(MouseEvent.MOUSE_UP,this.mouseUpEventHandler);
		}
		
		private function mouseUpEventHandler(event:MouseEvent):void
		{
			var infoObj_object:Object=new Object;
			infoObj_object.message = "mouseUpEvent";
			infoObj_object.event = event;
			
			trace("TabbedLessonView------->event:mouseup :"+event+" infoObj.event :"+infoObj_object.event);
			this.sendInfoToController(infoObj_object);
		}
		//----------------------------------------------------------------------------------------------------------------------------//
		private function informEditModeToModel():void
		{
			var infoObj_object:Object = new Object;
			infoObj_object.message = "setEditModeStatus";
			infoObj_object.editMode = this.editMode_bool;
			this.sendInfoToController(infoObj_object);
			
		}
		
		private function initComponentList(event:MouseEvent):void
		{
			this.component_list.visible = true;
			this.component_button.removeEventListener(MouseEvent.CLICK,this.initComponentList);
			this.component_button.addEventListener(MouseEvent.CLICK,this.removeComponentList);
		}
		
		private function removeComponentList(event:MouseEvent):void
		{
			this.component_list.visible=false;
			this.component_button.removeEventListener(MouseEvent.CLICK,this.removeComponentList);
			this.component_button.addEventListener(MouseEvent.CLICK,this.initComponentList);
		}
		
		private function listItemSelected(event:Event):void
		{
			trace(this.component_list.selectedItem.toString());
			this.addComponent_button.visible=true;
			
		}
		
		private function addComponent(event:MouseEvent):void
		{
			this.addComponent_button.visible=false;
			var infoObj_object:Object = new Object;
			infoObj_object.message = "openNewComponent";
			infoObj_object.name = this.component_list.selectedItem.toString();
			this.sendInfoToController(infoObj_object);
		}
		
		private function closeTab():void
		{
			//this function is here to enable saving before closing a tab
			Alert.show("Do you really want to close this tab","Notification",Alert.YES|Alert.NO,null,this.closeConfirmationHandler,null,Alert.YES);
			//if(this.editMode_bool)
				
		}
		
		private function closeConfirmationHandler(event:CloseEvent):void
		{
			if(event.detail==Alert.YES)
			{
				this.checkEditModeStatus();
			} 
			else
			{
				//do nothin
			}
		}
		
		private function checkEditModeStatus():void
		{
			if(this.editMode_bool)
			{
				Alert.show("Do you want to save the Contents of the tab","Notification",Alert.YES|Alert.NO,null,this.saveConfirmationHandler,null,Alert.YES);
			}
			else
				this.closeLesson();
		}
		private function saveConfirmationHandler(event:CloseEvent):void
		{
			 if(event.detail==Alert.YES)
			{ 
				var infoObj_object:Object = new Object;
				infoObj_object.message = "saveLesson";
				this.sendInfoToController(infoObj_object);
			}
			/* else
			{
				
			} */
			this.closeLesson();
		}
		
		private function closeLesson():void
		{
			var infoObj_object:Object = new Object;
			infoObj_object.message = "closeTabbedLesson";
			this.sendInfoToController(infoObj_object);
		}
		
		public function addChildToStage(displayObj:DisplayObject):void  //function to be used by lesson components to add themselves on stage
		{
			this.lessonTab.addChildren(displayObj);
		}
		
		public function removeChildFromStage(displayObj:DisplayObject):void
		{
			this.lessonTab.removeChild(displayObj);
		}
		//----------------------------Communication Functions------------------------------------------------------------//
		
		private function sendInfoToController(infoObj_object:Object):void
		{
			this.tabLessoncontroller.incomingMessageGateway(infoObj_object);
		}
		
		
		override public function update(model:Observable, infoObj_object:Object):void
		{
			trace("TabbedLessonView ----> Update recieved from model");
			var message_string:String = infoObj_object.message.toString();
			trace("TabbedLessonView ----> message : "+message_string);
			
			switch(message_string)
			{
				case "closeTab":
					this.closeTab();
					break;
			}
		}
		
	}
}